﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ServicesSheet_OfficeSuppliesList : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            NewDataSet();

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/办公用品申请列表.htm";
            master.PageName = "办公品申购列表";
            master.PicUrl = "";
            master.IntiBar();
        }

    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_OfficeSuppliesList));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.View_Services_OfficeSupplies";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        NewDataSet();
    }

  

    public String GetYearMonth(object InYear, object InMonth)
    {
        int IY = (int)InYear;
        int IM = (int)InMonth;
        return IY.ToString() + "/" + IM.ToString();
    }

    private void NewDataSet()
    {
        DataSet Tmp;
        string Sql = "";

        string strEmpId = SelUser.EmpName;
        string strFlow = this.QueryFlowFlag.SelectedValue;
        string strDep = this.DepartmentDList1.SelectDepmanetId;
        string strYear = this.DSelYearMonth1.SelectYear;
        string strMonth = this.DSelYearMonth1.SelectMonth;

        InitializeComponent();

        Sql = Sql + " SELECT Services_OfficeSupplies.SuppliesId, Services_OfficeSupplies.SuppliesEmpId,";
        Sql = Sql + " Services_OfficeSupplies.SuppliesYear, Services_OfficeSupplies.SuppliesMonth, ";
        Sql = Sql + " Services_OfficeSupplies.FlowFlag, Services_DetailsSupplies.DetailsId, ";
        Sql = Sql + " Services_DetailsSupplies.SId, Services_SV_Supplies.SType,";//Services_DetailsSupplies.SType, ";2008-11-6修改
        Sql = Sql + " Services_DetailsSupplies.Counts,";
        Sql = Sql + "  (SELECT SUM( Services_OfficeSupplies_Stock.SumPrice) AS SAmountc ";
        Sql = Sql + " FROM Services_OfficeSupplies_Stock ";
        Sql = Sql + " WHERE (Services_OfficeSupplies_Stock.Operate = 1) AND ";
        Sql = Sql + "     (Services_OfficeSupplies_Stock.SId = Services_DetailsSupplies.SId)";
        Sql = Sql + "  GROUP BY Services_OfficeSupplies_Stock.SId) AS SAmount, ";
        Sql = Sql + " Services_SV_Supplies.SName, Employees.EmpName, Employees.DepartmentId, ";
        Sql = Sql + " Department.DepartmentName,Services_DetailsSupplies.IsConfirm";
        Sql = Sql + " FROM Services_OfficeSupplies INNER JOIN ";
        Sql = Sql + "  Services_DetailsSupplies ON ";
        Sql = Sql + " Services_OfficeSupplies.SuppliesId = Services_DetailsSupplies.SuppliesId INNER JOIN ";
        Sql = Sql + " Employees ON ";
        Sql = Sql + " Services_OfficeSupplies.SuppliesEmpId = Employees.EmployeeID INNER JOIN ";
        Sql = Sql + " Services_SV_Supplies ON ";
        Sql = Sql + " Services_DetailsSupplies.SId = Services_SV_Supplies.SId INNER JOIN ";
        Sql = Sql + " Department ON Employees.DepartmentId = Department.DepartmentId ";
        Sql = Sql + " Where (1=1) ";
        if (strEmpId != "" && strEmpId != string.Empty) Sql = Sql + " AND Employees.EmpName like '%" + strEmpId + "%' ";
        if (strDep != "" && strDep != string.Empty && strDep != "0") Sql = Sql + " AND Employees.DepartmentId  Like '" + strDep + "%'";
        if (strFlow != "" && strFlow != string.Empty) Sql = Sql + " AND Services_OfficeSupplies.FlowFlag ='" + strFlow + "'";
        if (strYear != "" && strYear != "0" && strYear != string.Empty) Sql = Sql + string.Format(" AND Services_OfficeSupplies.SuppliesYear ={0}  ", strYear);
        if (strMonth != "" && strMonth != string.Empty && strMonth != "0") Sql = Sql + string.Format(" And Services_OfficeSupplies.SuppliesMonth={0} ", strMonth);
        //Sql += " AND Services_OfficeSupplies.FlowFlag='Z'";
        Sql = Sql + " GROUP BY Services_OfficeSupplies.SuppliesId, Services_OfficeSupplies.SuppliesEmpId, ";
        Sql = Sql + " Services_OfficeSupplies.SuppliesYear, Services_OfficeSupplies.SuppliesMonth, ";
        Sql = Sql + " Services_OfficeSupplies.FlowFlag, Services_DetailsSupplies.DetailsId, ";
        Sql = Sql + " Services_DetailsSupplies.SId, Services_SV_Supplies.SType,";//Services_DetailsSupplies.SType, ";2008-11-6修改
        Sql = Sql + " Services_DetailsSupplies.Counts, Services_SV_Supplies.SName, ";
        Sql = Sql + " Employees.EmpName, Employees.DepartmentId, Department.DepartmentName,Services_DetailsSupplies.IsConfirm";

        Tmp = WMaster.Execute(Sql);

        DataTable svTable;
        svTable = MakeNewWTable();
        DataRow SR;
        Single AllAmount = 0;
        int AllCount = 0;

        for (int i = 0; i < Tmp.Tables[0].Rows.Count; i++)
        {
            SR = svTable.NewRow();

            SR["SuppliesId"] = Tmp.Tables[0].Rows[i]["SuppliesId"];
            SR["DetailsId"] = Tmp.Tables[0].Rows[i]["DetailsId"];

            SR["SuppliesYear"] = GetYearMonth(Tmp.Tables[0].Rows[i]["SuppliesYear"], Tmp.Tables[0].Rows[i]["SuppliesMonth"]);//
            SR["EmpName"] = Tmp.Tables[0].Rows[i]["EmpName"];
            SR["DepartmentName"] = Tmp.Tables[0].Rows[i]["DepartmentName"];
            SR["SName"] = Tmp.Tables[0].Rows[i]["SName"];
            SR["SId"] = Tmp.Tables[0].Rows[i]["SId"];
            SR["SType"] = Tmp.Tables[0].Rows[i]["SType"];
            SR["Counts"] = Tmp.Tables[0].Rows[i]["Counts"];
            SR["IsConfirm"] = Tmp.Tables[0].Rows[i]["IsConfirm"];
            object Cb = Tmp.Tables[0].Rows[i]["Counts"];
            int Co = Convert.IsDBNull(Cb) ? 0 : Convert.ToInt32(Cb);
            AllCount = AllCount + Co;
            SR["FlowFlag"] = Tmp.Tables[0].Rows[i]["FlowFlag"];
            object ObSa = Tmp.Tables[0].Rows[i]["SAmount"];
            Single sA = Convert.IsDBNull(ObSa) ? 0 : Convert.ToSingle(ObSa);
            SR["SAmount"] = (sA == 0) ? "" : "￥" + sA.ToString(".##");
            Single ADs = (sA > 0 && Co > 0) ? sA / Co : 0;
            SR["Amount"] = (ADs == 0) ? "" : "￥" + ADs.ToString(".##");
            AllAmount = AllAmount + sA;
            svTable.Rows.Add(SR);
        }

        SR = svTable.NewRow();
        SR["SName"] = "合计";
        SR["Counts"] = AllCount;
        SR["SAmount"] = (AllAmount == 0) ? "" : "￥" + AllAmount.ToString(".##");
        svTable.Rows.Add(SR);

        wgvMaster.DataSource = svTable;
        wgvMaster.DataBind();

    }

    private DataTable MakeNewWTable()
    {
        DataTable WTable = new DataTable("WMaster");

        WTable.Columns.Add(new DataColumn("SuppliesId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("DetailsId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("SuppliesYear", typeof(string)));
        WTable.Columns.Add(new DataColumn("EmpName", typeof(string)));
        WTable.Columns.Add(new DataColumn("DepartmentName", typeof(string)));
        WTable.Columns.Add(new DataColumn("SId", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("SName", typeof(string)));
        WTable.Columns.Add(new DataColumn("SType", typeof(string)));
        WTable.Columns.Add(new DataColumn("Counts", typeof(Int32)));
        WTable.Columns.Add(new DataColumn("Amount", typeof(string)));
        WTable.Columns.Add(new DataColumn("SAmount", typeof(string)));
        WTable.Columns.Add(new DataColumn("FlowFlag", typeof(string)));
        WTable.Columns.Add(new DataColumn("IsConfirm", typeof(Boolean)));

        return WTable;

    }

    protected void wgvMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        wgvMaster.PageIndex = e.NewPageIndex;
        NewDataSet();
    }
}
